package com.fc.codegen.controller;

import com.fc.codegen.dto.JasyptDto;
import com.fc.common.core.utils.R;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 加解密接口
 * @ClassName: GenJasyptController
 * @Author: weisn
 * @Date: 2023/2/15 15:10
 * @Version: 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/jasypt")
@Tag(name = "加解密接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GenJasyptController {
    private final StringEncryptor stringEncryptor;

    @PostMapping(value = "encrypt")
    public R encrypt(@RequestBody JasyptDto jasyptDto) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 设置加密盐值(秘钥)
        encryptor.setPassword(jasyptDto.encKey);
        String secret = stringEncryptor.encrypt(jasyptDto.getEncValue());
        return R.ok(secret);
    }

    @PostMapping(value = "decrypt")
    public R decrypt(@RequestBody JasyptDto jasyptDto) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 设置加密盐值(秘钥)
        encryptor.setPassword(jasyptDto.encKey);
        String secret = stringEncryptor.decrypt(jasyptDto.getEncValue());
        return R.ok(secret);
    }


}
